package com.ruoyi.functiontest.controller;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.functiontest.domain.InterchangeMileageStatTest;
import com.ruoyi.functiontest.service.IInterchangeMileageStatTestService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 互通里程实测数据测试Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/functiontest/InterchangeMileageStatTest")
public class InterchangeMileageStatTestController extends BaseController
{
    @Autowired
    private IInterchangeMileageStatTestService interchangeMileageStatTestService;

    /**
     * 查询互通里程实测数据测试列表
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:list')")
    @GetMapping("/list")
    public AjaxResult list(InterchangeMileageStatTest interchangeMileageStatTest)
    {
        List<InterchangeMileageStatTest> list = interchangeMileageStatTestService.selectInterchangeMileageStatTestList(interchangeMileageStatTest);
        
        // 判断是否有搜索条件，如果有则构建树形结构返回
        boolean hasSearchCondition = interchangeMileageStatTest.getRoadName() != null && !interchangeMileageStatTest.getRoadName().isEmpty() ||
                                   interchangeMileageStatTest.getNodeName() != null && !interchangeMileageStatTest.getNodeName().isEmpty();
        
        if (hasSearchCondition) {
            // 有搜索条件时，构建树形结构返回，包含匹配节点及其所有子节点
            List<InterchangeMileageStatTest> treeList = buildTree(list);
            return success(treeList);
        } else {
            // 无搜索条件时，返回扁平列表
            return success(list);
        }
    }

    /**
     * 导出互通里程实测数据测试列表
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:export')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InterchangeMileageStatTest interchangeMileageStatTest)
    {
        List<InterchangeMileageStatTest> list = interchangeMileageStatTestService.selectInterchangeMileageStatTestList(interchangeMileageStatTest);
        
        // 构建树形结构
        List<InterchangeMileageStatTest> treeList = buildTree(list);
        
        // 按照树形结构层次排序导出所有节点
        List<InterchangeMileageStatTest> exportList = new ArrayList<>();
        flattenTree(treeList, exportList);
        
        ExcelUtil<InterchangeMileageStatTest> util = new ExcelUtil<InterchangeMileageStatTest>(InterchangeMileageStatTest.class);
        util.exportExcel(response, exportList, "互通里程实测数据测试数据");
    }

    /**
     * 构建树形结构
     */
    private List<InterchangeMileageStatTest> buildTree(List<InterchangeMileageStatTest> list) {
        // 构建父子关系映射
        Map<Long, InterchangeMileageStatTest> nodeMap = new HashMap<>();
        List<InterchangeMileageStatTest> rootNodes = new ArrayList<>();
        
        // 先将所有节点放入map中
        for (InterchangeMileageStatTest node : list) {
            nodeMap.put(node.getId(), node);
        }
        
        // 构建父子关系
        for (InterchangeMileageStatTest node : list) {
            Long parentId = node.getParentId();
            if (parentId != null && parentId != 0) {
                InterchangeMileageStatTest parent = nodeMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<InterchangeMileageStatTest>());
                    }
                    @SuppressWarnings("unchecked")
                    List<InterchangeMileageStatTest> children = (List<InterchangeMileageStatTest>) parent.getChildren();
                    children.add(node);
                }
            } else {
                rootNodes.add(node);
            }
        }
        
        return rootNodes;
    }

    /**
     * 将树形结构按照层次顺序展平为列表
     */
    private void flattenTree(List<InterchangeMileageStatTest> treeList, List<InterchangeMileageStatTest> resultList) {
        for (InterchangeMileageStatTest node : treeList) {
            // 添加当前节点到结果列表
            resultList.add(node);
            
            // 递归处理子节点
            if (node.getChildren() != null && !node.getChildren().isEmpty()) {
                @SuppressWarnings("unchecked")
                List<InterchangeMileageStatTest> children = (List<InterchangeMileageStatTest>) node.getChildren();
                flattenTree(children, resultList);
            }
        }
    }

    /**
     * 获取互通里程实测数据测试详细信息
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(interchangeMileageStatTestService.selectInterchangeMileageStatTestById(id));
    }

    /**
     * 新增互通里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:add')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return toAjax(interchangeMileageStatTestService.insertInterchangeMileageStatTest(interchangeMileageStatTest));
    }

    /**
     * 修改互通里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:edit')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InterchangeMileageStatTest interchangeMileageStatTest)
    {
        return toAjax(interchangeMileageStatTestService.updateInterchangeMileageStatTest(interchangeMileageStatTest));
    }

    /**
     * 删除互通里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:remove')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 检查是否可以删除节点（父节点必须在子节点删除完后才能删除）
        for (Long id : ids) {
            if (interchangeMileageStatTestService.hasChildById(id)) {
                return AjaxResult.error("节点存在子节点，请先删除子节点后再删除父节点");
            }
        }
        return toAjax(interchangeMileageStatTestService.deleteInterchangeMileageStatTestByIds(ids));
    }
    
    /**
     * 导入互通里程实测数据测试数据
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:import')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<InterchangeMileageStatTest> util = new ExcelUtil<InterchangeMileageStatTest>(InterchangeMileageStatTest.class);
        List<InterchangeMileageStatTest> importList = util.importExcel(file.getInputStream());
        
        // 将扁平数据转换为树形结构并保存
        String message = interchangeMileageStatTestService.importInterchangeMileageStatTest(importList, updateSupport);
        return success(message);
    }

    /**
     * 导入模板下载
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        List<InterchangeMileageStatTest> templateList = new ArrayList<>();
        
        // 六安根节点
        InterchangeMileageStatTest liuan = new InterchangeMileageStatTest();
        liuan.setId(1L);
        liuan.setParentId(0L);
        liuan.setRoadName("六安");
        liuan.setNodeName("");
        liuan.setRamp1Type("");
        liuan.setRamp1Length(new java.math.BigDecimal("0"));
        liuan.setRamp2Type("");
        liuan.setRamp2Length(new java.math.BigDecimal("0"));
        liuan.setRamp3Type("");
        liuan.setRamp3Length(new java.math.BigDecimal("0"));
        liuan.setRamp4Type("");
        liuan.setRamp4Length(new java.math.BigDecimal("0"));
        liuan.setRampAvgLength(new java.math.BigDecimal("0"));
        liuan.setTollCenterLength(new java.math.BigDecimal("0"));
        liuan.setApproachLength(new java.math.BigDecimal("0"));
        templateList.add(liuan);
        
        // 月亮河收费站
        InterchangeMileageStatTest yuelianghe = new InterchangeMileageStatTest();
        yuelianghe.setId(2L);
        yuelianghe.setParentId(1L);
        yuelianghe.setRoadName("");
        yuelianghe.setNodeName("月亮河收费站");
        yuelianghe.setRamp1Type("A匝道");
        yuelianghe.setRamp1Length(new java.math.BigDecimal("542.72"));
        yuelianghe.setRamp2Type("B匝道");
        yuelianghe.setRamp2Length(new java.math.BigDecimal("774.35"));
        yuelianghe.setRamp3Type("C匝道");
        yuelianghe.setRamp3Length(new java.math.BigDecimal("384.25"));
        yuelianghe.setRamp4Type("D匝道");
        yuelianghe.setRamp4Length(new java.math.BigDecimal("942.42"));
        yuelianghe.setRampAvgLength(new java.math.BigDecimal("660.94"));
        yuelianghe.setTollCenterLength(new java.math.BigDecimal("243.63"));
        yuelianghe.setApproachLength(new java.math.BigDecimal("220.13"));
        templateList.add(yuelianghe);
        
        // 郎岱收费站
        InterchangeMileageStatTest langdai = new InterchangeMileageStatTest();
        langdai.setId(3L);
        langdai.setParentId(1L);
        langdai.setRoadName("");
        langdai.setNodeName("郎岱收费站");
        langdai.setRamp1Type("A匝道");
        langdai.setRamp1Length(new java.math.BigDecimal("521.69"));
        langdai.setRamp2Type("B匝道");
        langdai.setRamp2Length(new java.math.BigDecimal("993.01"));
        langdai.setRamp3Type("C匝道");
        langdai.setRamp3Length(new java.math.BigDecimal("367.28"));
        langdai.setRamp4Type("D匝道");
        langdai.setRamp4Length(new java.math.BigDecimal("928.25"));
        langdai.setRampAvgLength(new java.math.BigDecimal("702.56"));
        langdai.setTollCenterLength(new java.math.BigDecimal("185.38"));
        langdai.setApproachLength(new java.math.BigDecimal("3487.72"));
        templateList.add(langdai);
        
        // 关岭北收费站
        InterchangeMileageStatTest guanlingbei = new InterchangeMileageStatTest();
        guanlingbei.setId(4L);
        guanlingbei.setParentId(1L);
        guanlingbei.setRoadName("");
        guanlingbei.setNodeName("关岭北收费站");
        guanlingbei.setRamp1Type("A匝道");
        guanlingbei.setRamp1Length(new java.math.BigDecimal("920.13"));
        guanlingbei.setRamp2Type("B匝道");
        guanlingbei.setRamp2Length(new java.math.BigDecimal("571.43"));
        guanlingbei.setRamp3Type("C匝道");
        guanlingbei.setRamp3Length(new java.math.BigDecimal("844.52"));
        guanlingbei.setRamp4Type("D匝道");
        guanlingbei.setRamp4Length(new java.math.BigDecimal("548.55"));
        guanlingbei.setRampAvgLength(new java.math.BigDecimal("721.16"));
        guanlingbei.setTollCenterLength(new java.math.BigDecimal("116.94"));
        guanlingbei.setApproachLength(new java.math.BigDecimal("1928.31"));
        templateList.add(guanlingbei);
        
        // 关岭南收费站
        InterchangeMileageStatTest guanlingnan = new InterchangeMileageStatTest();
        guanlingnan.setId(5L);
        guanlingnan.setParentId(1L);
        guanlingnan.setRoadName("");
        guanlingnan.setNodeName("关岭南收费站");
        guanlingnan.setRamp1Type("A匝道");
        guanlingnan.setRamp1Length(new java.math.BigDecimal("503.86"));
        guanlingnan.setRamp2Type("B匝道");
        guanlingnan.setRamp2Length(new java.math.BigDecimal("1012.15"));
        guanlingnan.setRamp3Type("C匝道");
        guanlingnan.setRamp3Length(new java.math.BigDecimal("485.14"));
        guanlingnan.setRamp4Type("D匝道");
        guanlingnan.setRamp4Length(new java.math.BigDecimal("994.71"));
        guanlingnan.setRampAvgLength(new java.math.BigDecimal("748.97"));
        guanlingnan.setTollCenterLength(new java.math.BigDecimal("220.97"));
        guanlingnan.setApproachLength(new java.math.BigDecimal("803.59"));
        templateList.add(guanlingnan);
        
        // 花江收费站
        InterchangeMileageStatTest huajiang = new InterchangeMileageStatTest();
        huajiang.setId(6L);
        huajiang.setParentId(1L);
        huajiang.setRoadName("");
        huajiang.setNodeName("花江收费站");
        huajiang.setRamp1Type("A匝道");
        huajiang.setRamp1Length(new java.math.BigDecimal("995.78"));
        huajiang.setRamp2Type("B匝道");
        huajiang.setRamp2Length(new java.math.BigDecimal("518.86"));
        huajiang.setRamp3Type("C匝道");
        huajiang.setRamp3Length(new java.math.BigDecimal("930.08"));
        huajiang.setRamp4Type("D匝道");
        huajiang.setRamp4Length(new java.math.BigDecimal("359.70"));
        huajiang.setRampAvgLength(new java.math.BigDecimal("701.10"));
        huajiang.setTollCenterLength(new java.math.BigDecimal("441.07"));
        huajiang.setApproachLength(new java.math.BigDecimal("190.53"));
        templateList.add(huajiang);
        
        // 云渡收费站
        InterchangeMileageStatTest yundu = new InterchangeMileageStatTest();
        yundu.setId(7L);
        yundu.setParentId(1L);
        yundu.setRoadName("");
        yundu.setNodeName("云渡收费站");
        yundu.setRamp1Type("");
        yundu.setRamp1Length(new java.math.BigDecimal("0"));
        yundu.setRamp2Type("");
        yundu.setRamp2Length(new java.math.BigDecimal("0"));
        yundu.setRamp3Type("");
        yundu.setRamp3Length(new java.math.BigDecimal("0"));
        yundu.setRamp4Type("");
        yundu.setRamp4Length(new java.math.BigDecimal("0"));
        yundu.setRampAvgLength(new java.math.BigDecimal("0"));
        yundu.setTollCenterLength(new java.math.BigDecimal("0"));
        yundu.setApproachLength(new java.math.BigDecimal("0"));
        templateList.add(yundu);
        
        // 小花江收费站
        InterchangeMileageStatTest xiaohuajiang = new InterchangeMileageStatTest();
        xiaohuajiang.setId(8L);
        xiaohuajiang.setParentId(1L);
        xiaohuajiang.setRoadName("");
        xiaohuajiang.setNodeName("小花江收费站");
        xiaohuajiang.setRamp1Type("A匝道");
        xiaohuajiang.setRamp1Length(new java.math.BigDecimal("468.33"));
        xiaohuajiang.setRamp2Type("B匝道");
        xiaohuajiang.setRamp2Length(new java.math.BigDecimal("990.83"));
        xiaohuajiang.setRamp3Type("C匝道");
        xiaohuajiang.setRamp3Length(new java.math.BigDecimal("484.34"));
        xiaohuajiang.setRamp4Type("D匝道");
        xiaohuajiang.setRamp4Length(new java.math.BigDecimal("817.07"));
        xiaohuajiang.setRampAvgLength(new java.math.BigDecimal("690.14"));
        xiaohuajiang.setTollCenterLength(new java.math.BigDecimal("175.35"));
        xiaohuajiang.setApproachLength(new java.math.BigDecimal("329.88"));
        templateList.add(xiaohuajiang);
        
        // 纳晴根节点
        InterchangeMileageStatTest naqing = new InterchangeMileageStatTest();
        naqing.setId(9L);
        naqing.setParentId(0L);
        naqing.setRoadName("纳晴");
        naqing.setNodeName("");
        naqing.setRamp1Type("");
        naqing.setRamp1Length(new java.math.BigDecimal("0"));
        naqing.setRamp2Type("");
        naqing.setRamp2Length(new java.math.BigDecimal("0"));
        naqing.setRamp3Type("");
        naqing.setRamp3Length(new java.math.BigDecimal("0"));
        naqing.setRamp4Type("");
        naqing.setRamp4Length(new java.math.BigDecimal("0"));
        naqing.setRampAvgLength(new java.math.BigDecimal("0"));
        naqing.setTollCenterLength(new java.math.BigDecimal("0"));
        naqing.setApproachLength(new java.math.BigDecimal("0"));
        templateList.add(naqing);
        
        // 北盘江收费站
        InterchangeMileageStatTest beipanjiang = new InterchangeMileageStatTest();
        beipanjiang.setId(10L);
        beipanjiang.setParentId(9L);
        beipanjiang.setRoadName("");
        beipanjiang.setNodeName("北盘江收费站");
        beipanjiang.setRamp1Type("A匝道");
        beipanjiang.setRamp1Length(new java.math.BigDecimal("901.02"));
        beipanjiang.setRamp2Type("B匝道");
        beipanjiang.setRamp2Length(new java.math.BigDecimal("592.05"));
        beipanjiang.setRamp3Type("C匝道");
        beipanjiang.setRamp3Length(new java.math.BigDecimal("901.34"));
        beipanjiang.setRamp4Type("D匝道");
        beipanjiang.setRamp4Length(new java.math.BigDecimal("510.99"));
        beipanjiang.setRampAvgLength(new java.math.BigDecimal("726.35"));
        beipanjiang.setTollCenterLength(new java.math.BigDecimal("251.14"));
        beipanjiang.setApproachLength(new java.math.BigDecimal("674.59"));
        templateList.add(beipanjiang);
        
        // 双乳峰收费站
        InterchangeMileageStatTest shuangrufeng = new InterchangeMileageStatTest();
        shuangrufeng.setId(11L);
        shuangrufeng.setParentId(9L);
        shuangrufeng.setRoadName("");
        shuangrufeng.setNodeName("双乳峰收费站");
        shuangrufeng.setRamp1Type("A匝道");
        shuangrufeng.setRamp1Length(new java.math.BigDecimal("911.09"));
        shuangrufeng.setRamp2Type("B匝道");
        shuangrufeng.setRamp2Length(new java.math.BigDecimal("899.10"));
        shuangrufeng.setRamp3Type("C匝道");
        shuangrufeng.setRamp3Length(new java.math.BigDecimal("561.06"));
        shuangrufeng.setRamp4Type("D匝道");
        shuangrufeng.setRamp4Length(new java.math.BigDecimal("518.56"));
        shuangrufeng.setRampAvgLength(new java.math.BigDecimal("722.45"));
        shuangrufeng.setTollCenterLength(new java.math.BigDecimal("102.64"));
        shuangrufeng.setApproachLength(new java.math.BigDecimal("1040.00"));
        templateList.add(shuangrufeng);
        
        // 笃山收费站
        InterchangeMileageStatTest dushan = new InterchangeMileageStatTest();
        dushan.setId(12L);
        dushan.setParentId(9L);
        dushan.setRoadName("");
        dushan.setNodeName("笃山收费站");
        dushan.setRamp1Type("A匝道");
        dushan.setRamp1Length(new java.math.BigDecimal("497.95"));
        dushan.setRamp2Type("B匝道");
        dushan.setRamp2Length(new java.math.BigDecimal("383.74"));
        dushan.setRamp3Type("C匝道");
        dushan.setRamp3Length(new java.math.BigDecimal("687.08"));
        dushan.setRamp4Type("D匝道");
        dushan.setRamp4Length(new java.math.BigDecimal("831.89"));
        dushan.setRampAvgLength(new java.math.BigDecimal("600.16"));
        dushan.setTollCenterLength(new java.math.BigDecimal("244.21"));
        dushan.setApproachLength(new java.math.BigDecimal("320.00"));
        templateList.add(dushan);
        
        // 安龙北收费站
        InterchangeMileageStatTest anlongbei = new InterchangeMileageStatTest();
        anlongbei.setId(13L);
        anlongbei.setParentId(9L);
        anlongbei.setRoadName("");
        anlongbei.setNodeName("安龙北收费站");
        anlongbei.setRamp1Type("A匝道");
        anlongbei.setRamp1Length(new java.math.BigDecimal("766.22"));
        anlongbei.setRamp2Type("B匝道");
        anlongbei.setRamp2Length(new java.math.BigDecimal("610.19"));
        anlongbei.setRamp3Type("C匝道");
        anlongbei.setRamp3Length(new java.math.BigDecimal("493.15"));
        anlongbei.setRamp4Type("D匝道");
        anlongbei.setRamp4Length(new java.math.BigDecimal("354.86"));
        anlongbei.setRampAvgLength(new java.math.BigDecimal("556.11"));
        anlongbei.setTollCenterLength(new java.math.BigDecimal("325.51"));
        anlongbei.setApproachLength(new java.math.BigDecimal("726.77"));
        templateList.add(anlongbei);
        
        // 牂牁江北收费站
        InterchangeMileageStatTest zangkejiangbei = new InterchangeMileageStatTest();
        zangkejiangbei.setId(14L);
        zangkejiangbei.setParentId(9L);
        zangkejiangbei.setRoadName("");
        zangkejiangbei.setNodeName("牂牁江北收费站");
        zangkejiangbei.setRamp1Type("");
        zangkejiangbei.setRamp1Length(new java.math.BigDecimal("0"));
        zangkejiangbei.setRamp2Type("B匝道");
        zangkejiangbei.setRamp2Length(new java.math.BigDecimal("950.40"));
        zangkejiangbei.setRamp3Type("C匝道");
        zangkejiangbei.setRamp3Length(new java.math.BigDecimal("1175.58"));
        zangkejiangbei.setRamp4Type("D匝道");
        zangkejiangbei.setRamp4Length(new java.math.BigDecimal("835.95"));
        zangkejiangbei.setRampAvgLength(new java.math.BigDecimal("1010.65"));
        zangkejiangbei.setTollCenterLength(new java.math.BigDecimal("1131.41"));
        zangkejiangbei.setApproachLength(new java.math.BigDecimal("2501.65"));
        templateList.add(zangkejiangbei);
        
        // 长流收费站
        InterchangeMileageStatTest changliu = new InterchangeMileageStatTest();
        changliu.setId(15L);
        changliu.setParentId(9L);
        changliu.setRoadName("");
        changliu.setNodeName("长流收费站");
        changliu.setRamp1Type("");
        changliu.setRamp1Length(new java.math.BigDecimal("0"));
        changliu.setRamp2Type("B匝道");
        changliu.setRamp2Length(new java.math.BigDecimal("1031.27"));
        changliu.setRamp3Type("C匝道");
        changliu.setRamp3Length(new java.math.BigDecimal("912.74"));
        changliu.setRamp4Type("D匝道");
        changliu.setRamp4Length(new java.math.BigDecimal("936.11"));
        changliu.setRampAvgLength(new java.math.BigDecimal("921.90"));
        changliu.setTollCenterLength(new java.math.BigDecimal("658.63"));
        changliu.setApproachLength(new java.math.BigDecimal("4554.23"));
        templateList.add(changliu);
        
        // 中营收费站
        InterchangeMileageStatTest zhongying = new InterchangeMileageStatTest();
        zhongying.setId(16L);
        zhongying.setParentId(9L);
        zhongying.setRoadName("");
        zhongying.setNodeName("中营收费站");
        zhongying.setRamp1Type("");
        zhongying.setRamp1Length(new java.math.BigDecimal("0"));
        zhongying.setRamp2Type("B匝道");
        zhongying.setRamp2Length(new java.math.BigDecimal("862.56"));
        zhongying.setRamp3Type("C匝道");
        zhongying.setRamp3Length(new java.math.BigDecimal("535.62"));
        zhongying.setRamp4Type("D匝道");
        zhongying.setRamp4Length(new java.math.BigDecimal("786.47"));
        zhongying.setRampAvgLength(new java.math.BigDecimal("846.67"));
        zhongying.setTollCenterLength(new java.math.BigDecimal("115.46"));
        zhongying.setApproachLength(new java.math.BigDecimal("261.11"));
        templateList.add(zhongying);
        
        ExcelUtil<InterchangeMileageStatTest> util = new ExcelUtil<InterchangeMileageStatTest>(InterchangeMileageStatTest.class);
        util.exportExcel(response, templateList, "互通里程实测数据测试导入模板");
    }
    
    /**
     * 实测单位批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:fieldAudit')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/fieldAudit/batch")
    public AjaxResult fieldAuditBatch()
    {
        try {
            // 批量更新所有记录的review1字段为0
            int updatedCount = interchangeMileageStatTestService.updateReview1ToZero();
            System.out.println("实测单位批量审核完成，更新记录数：" + updatedCount);
            return success("实测单位批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("实测单位批量审核失败：" + e.getMessage());
            return error("实测单位批量审核失败：" + e.getMessage());
        }
    }
    
    /**
     * 业主批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:ownerAudit')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/ownerAudit/batch")
    public AjaxResult ownerAuditBatch(@RequestBody Map<String, Object> params)
    {
        try {
            Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
            Long roleId = params.get("roleId") != null ? Long.valueOf(params.get("roleId").toString()) : null;
            
            System.out.println("业主批量审核 - 审核所有数据，deptId: " + deptId + ", roleId: " + roleId);
            
            // 1. 校验所有数据的review1字段是否都为0
            boolean allReview1Zero = interchangeMileageStatTestService.checkAllReview1IsZero();
            if (!allReview1Zero) {
                return error("业主批量审核失败：存在未通过实测单位审核的数据，请先完成实测单位审核");
            }
            
            // 2. 构建查询条件
            InterchangeMileageStatTest queryCondition = new InterchangeMileageStatTest();
            queryCondition.setDeptId(deptId);
            queryCondition.setRoleId(roleId);
            
            // 3. 从params中提取查询条件
            if (params.get("roadName") != null) {
                queryCondition.setRoadName(params.get("roadName").toString());
            }
            if (params.get("nodeName") != null) {
                queryCondition.setNodeName(params.get("nodeName").toString());
            }
            
            // 4. 根据条件批量更新业主审核状态
            int updatedCount = interchangeMileageStatTestService.updateReview2ToZeroByCondition(queryCondition);
            
            System.out.println("业主批量审核完成，更新记录数：" + updatedCount);
            return success("业主批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("业主批量审核失败：" + e.getMessage());
            return error("业主批量审核失败：" + e.getMessage());
        }
    }
    
    /**
     * 中心批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:InterchangeMileageStatTest:centerAudit')")
    @Log(title = "互通里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/centerAudit/batch")
    public AjaxResult centerAuditBatch()
    {
        try {
            System.out.println("中心批量审核 - 审核所有数据");
            
            // 1. 校验所有数据的review1和review2字段是否都不为空且都为0
            boolean allReview1AndReview2Zero = interchangeMileageStatTestService.checkAllReview1AndReview2AreZero();
            if (!allReview1AndReview2Zero) {
                return error("中心批量审核失败：存在未通过实测单位审核或业主审核的数据，请先完成前序审核");
            }
            
            // 2. 批量更新中心审核状态
            int updatedCount = interchangeMileageStatTestService.updateReview3ToZero();
            
            System.out.println("中心批量审核完成，更新记录数：" + updatedCount);
            return success("中心批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("中心批量审核失败：" + e.getMessage());
            return error("中心批量审核失败：" + e.getMessage());
        }
    }
}
